﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class ListItems : Portal.Interface.WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "ListItems";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Title", "Danh sách ảnh");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Description", "");
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(ListItems));
            string AdvImageId = System.Web.HttpContext.Current.Request["AdvImageId"];


            PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new AdvImage().PermissionTypes, "AdvImage");
            AdvImageCls AdvImageCls = new AdvImageBLL().CreateModel(Environments.ActiveLanguage, AdvImageId);
            Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "ShowAdvImageItems", AdvImageId, AdvImageCls.Owner, true);

            string RetHtml =
                Portal.Environment.WebPartUtilities.AttachJavascript("Portal.CMS", "AdvImage.js") +
                 "<div class=cssFormContent id=divFormContent>" +
                     GetHtml(Environments.ActiveLanguage, AdvImageId, "", 0) +
                 "</div>";

            RetHtml = Environments.GetFormDialog(RetHtml, Title);
            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string GetHtml(string ActiveLanguage, string AdvImageId, string Keyword, int CurrentPageIndex)
        {
            string RetHtml = "";
            DataTable dtAdvImageItems = new AdvImageBLL().ReadingAdvImageItems(AdvImageId, Keyword);
            int TotalRow = dtAdvImageItems.Rows.Count;

            AdvImageCls AdvImageCls = new AdvImageBLL().CreateModel(Environments.ActiveLanguage, AdvImageId);
            if (AdvImageCls == null)
            {
                return Environment.Environments.GetRecordNotFound();
            }
            string TabModuleId = "71F21D98";
            int StartIndex = 0;
            int EndIndex = 0;
            string PagingText = new PagingCls().GetPaging(TabModuleId, dtAdvImageItems.Rows.Count, 5, 5, CurrentPageIndex, "ClientJumpAdvImageItem", ref StartIndex, ref EndIndex);

            string SelectLanguageText =
             "<select id=divSelectLanguage onchange=javascript:DetectAdvImageDescription('" + AdvImageId + "');>";
            string[] Languages = Environment.Environments.GetLanguages();
            for (int iIndex = 0; iIndex < Languages.Length; iIndex++)
            {
                if (ActiveLanguage.Equals(Languages[iIndex]))
                {
                    SelectLanguageText += "<option selected value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
                else
                {
                    SelectLanguageText += "<option value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
            }
            SelectLanguageText += "</select>";


            RetHtml +=
                             "<div class=cssAdvImageFunction>" + ResourceBLL.GetText(WebPartId, "SelectLanguage", "Chọn") + ":" + SelectLanguageText + ResourceBLL.GetText(WebPartId, "Keyword", "Từ khóa") + ":<input id=txtKeyword value=\"" + Keyword + "\"><input type=button onclick=javascript:ClientReading('" + AdvImageId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "Refresh", "Đọc lại") + "\"><input type=button onclick=javascript:UploadNewImage('" + AdvImageId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "UploadNewImage", "Upload ảnh mới") + "\"><input type=button class=cssCancelButton onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "ListItemsCancelButton", "Thoát") + "\"></div>" +
                             "<div id=divProcessing></div>" +
                             "<input type=hidden id=txtAdvImageId value=\"" + AdvImageId + "\">" +
                             PagingText +
                             "<div id=divProcessingAdvImageItem></div>";


            if (dtAdvImageItems.Rows.Count == 0)
            {
                RetHtml += "<div class=\"cssNoData\">" + ResourceBLL.GetText(WebPartId, "NoData", "Chưa có dữ liệu nào trong chuyên mục này!") + "</div>";
            }
            else
            {
                RetHtml +=
                    "<div class=cssAdvImageItems>" +
                    "<table>";

                for (int iIndex = StartIndex; iIndex < EndIndex; iIndex++)
                {
                    AdvImageItemCls AdvImageItemCls = new AdvImageItemCls();
                    AdvImageItemCls.AdvImageItemId = (string)dtAdvImageItems.Rows[iIndex]["AdvImageItemId"];
                    AdvImageItemCls.AdvImageItemName = "???";
                    try
                    {
                        AdvImageItemCls.AdvImageItemName = (string)dtAdvImageItems.Rows[iIndex][Environments.ActiveLanguage + "AdvImageItemName"];
                    }
                    catch { }
                    AdvImageItemCls.Url = "";
                    if (dtAdvImageItems.Rows[iIndex]["Url"] != DBNull.Value)
                    {
                        AdvImageItemCls.Url = (string)dtAdvImageItems.Rows[iIndex]["Url"];
                    }
                    try
                    {
                        AdvImageItemCls.Description = (string)dtAdvImageItems.Rows[iIndex]["Description"];
                    }
                    catch { }
                    AdvImageItemCls.Target = (string)dtAdvImageItems.Rows[iIndex]["Target"];
                    try
                    {
                        AdvImageItemCls.Alias = (string)dtAdvImageItems.Rows[iIndex]["Alias"];
                    }
                    catch { }

                    AdvImageItemCls.FileImage = "";
                    if (dtAdvImageItems.Rows[iIndex]["FileImage"] != DBNull.Value)
                    {
                        AdvImageItemCls.FileImage = (string)dtAdvImageItems.Rows[iIndex]["FileImage"];
                    }
                    AdvImageItemCls.IndexOrder = (int)dtAdvImageItems.Rows[iIndex]["IndexOrder"];
                    try
                    {
                        AdvImageItemCls.IsPublishing = (int)dtAdvImageItems.Rows[iIndex]["IsPublishing"];
                    }
                    catch { }
                    string belongSite = (string)dtAdvImageItems.Rows[iIndex]["belongSite"];
                    RetHtml += ServerSideNewLinkItem(AdvImageId, belongSite, iIndex, AdvImageItemCls);                        
                }
                RetHtml += "</table>" +                            
                         "</div>";
            }
            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideNewLinkItem(string AdvImageId, string belongSite, int Index, AdvImageItemCls AdvImageItemCls)
        {
            string PublishingText = "<input id=txtPublishing_" + AdvImageItemCls.AdvImageItemId + " type=button onclick=javascript:ClientPublishing('" + AdvImageItemCls.AdvImageItemId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "Publishing", "Xuất bản") + " (" + (Index + 1) + ")\">";
            if (AdvImageItemCls.IsPublishing == 1)
            {
                PublishingText = "<input id=txtPublishing_" + AdvImageItemCls.AdvImageItemId + " type=button onclick=javascript:ClientUnPublishing('" + AdvImageItemCls.AdvImageItemId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "UnPublishing", "Ngừng xuất bản") + "(" + (Index + 1) + ")\">";
            }
            string ImageUrl = Environments.HttpMediaRoot + "/media/" + Environments.PortalAlias + "/AdvImage/Images/" + belongSite + "/" + AdvImageId + "/crop/" + AdvImageItemCls.FileImage;
            //string ImageUrl = Environments.HttpMediaRoot + "/media/" + Environments.PortalAlias + "/AdvImage/Images/" + belongSite + "/" + AdvImageId + "/crop/" + AdvImageItemCls.FileImage;
            string HtmlContentUrl = "javascript:ClientHtmlItem('" + AdvImageId + "','" + AdvImageItemCls.AdvImageItemId + "');";
            string SyncContentUrl = "javascript:ClientSyncItem('" + AdvImageId + "','" + AdvImageItemCls.AdvImageItemId + "');";
            string AdvUploadUrl = "javascript:ClientUploadAvatar('" + AdvImageId + "','" + AdvImageItemCls.AdvImageItemId + "');";
            string
            Html = "<tr>" +
                   "<td valign=top><a target=_blank href=\"" + ImageUrl + "\"><img class=imgAdvImageItem src=\"" + ImageUrl + "\"></a></td>" +
                   "<td valign=top>" +
                   "<table>" +
                   "<tr>" +
                   "    <td>#</td>" +
                   "    <td class=cssListItemsCol1><input type=hidden value=\"" + AdvImageItemCls.AdvImageItemId + "\" id=\"txtItemId_" + Index + "\">" + (Index + 1) + "</td>\r\n" +
                   "</tr>" +
                   "<tr>" +
                   "    <td>" + ResourceBLL.GetText(WebPartId, "Image", "Ảnh") + ":</td>" +
                   "    <td class=cssListItemsCol1><input class=cssAdvImageItemTextBox value=\"" + AdvImageItemCls.AdvImageItemName + "\" id=\"txtItemName_" + Index + "\"></td>\r\n" +
                   "</tr>" +
                    "<tr>" +
                   "    <td>" + ResourceBLL.GetText(WebPartId, "Alias", "Alias") + ":</td>" +
                   "    <td class=cssListItemsCol2><input class=cssAdvImageItemTextBox value=\"" + AdvImageItemCls.Alias + "\" id=\"txtAlias_" + Index + "\"></td>\r\n" +
                   "</tr>" +
                   "<tr>" +
                   "    <td>" + ResourceBLL.GetText(WebPartId, "Url", "Url") + ":</td>" +
                   "    <td class=cssListItemsCol2><input class=cssAdvImageItemTextBox value=\"" + AdvImageItemCls.Url + "\" id=\"txtItemUrl_" + Index + "\"></td>\r\n" +
                   "</tr>" +
                   "<tr>" +
                   "    <td>" + ResourceBLL.GetText(WebPartId, "Target", "Target") + ":</td>" +
                   "    <td class=cssListItemsCol3><input class=cssAdvImageItemTextBox value=\"" + AdvImageItemCls.Target + "\"  id=\"txtItemTarget_" + Index + "\"></td>\r\n" +
                   "</tr>" +
                   "<tr>" +
                   "    <td>" + ResourceBLL.GetText(WebPartId, "Description", "Mô tả") + ":</td>" +
                   "    <td class=cssListItemsCol4><textarea class=cssAdvImageItemTextBox id=\"txtItemDescription_" + Index + "\">" + AdvImageItemCls.Description + "</textarea></td>\r\n" +
                   "</tr>" +
                   "<tr>" +
                   "    <td>Vị trí</td>" +
                   "    <td class=cssListItemsCol5><input class=cssAdvImageItemTextBox value=\"" + AdvImageItemCls.IndexOrder.ToString() + "\" value=\"" + (Index + 1) + "\" id=\"txtItemIndexOrder_" + Index + "\"></td>\r\n" +
                   "</tr>" +
                   "<tr>" +
                   "    <td></td>" +
                   "    <td class=cssListItemsCol6> <input onclick=javascript:PermissionAdvImageItem('" + AdvImageItemCls.AdvImageItemId + "'); type=button value=\"" + ResourceBLL.GetText(WebPartId, "PermissionButton", "Phân quyền") + " (" + (Index + 1) + ")\">&nbsp;<input onclick=javascript:SaveAdvImageItem(" + Index + ",'" + AdvImageItemCls.AdvImageItemId + "'); type=button value=\"" + ResourceBLL.GetText(WebPartId, "SaveButtonItem", "Ghi lại") + " (" + (Index + 1) + ")\">" + PublishingText + "<input onclick=javascript:DeleteAdvImageItem('" + AdvImageId + "','" + AdvImageItemCls.AdvImageItemId + "'); type=button value=\"" + ResourceBLL.GetText(WebPartId, "DeleteButton", "Xoá") + " (" + (Index + 1) + ")\"><input onclick=javascript:UpdateNewAdvImageItem('" + AdvImageId + "','" + AdvImageItemCls.AdvImageItemId + "'); type=button value=\"" + ResourceBLL.GetText(WebPartId, "UpdateButton", "Upload ảnh khác") + " (" + (Index + 1) + ")\"><input type=button value=\"" + ResourceBLL.GetText(WebPartId, "Html", "Html") + "(" + (Index + 1) + ")\" onclick=" + HtmlContentUrl + "><input type=button value=\"" + ResourceBLL.GetText(WebPartId, "Sync", "Sync") + "(" + (Index + 1) + ")\" onclick=" + SyncContentUrl + "><input type=button value=\"" + ResourceBLL.GetText(WebPartId, "UploadAvatar", "Avatar") + "(" + (Index + 1) + ")\" onclick=" + AdvUploadUrl + ">    </td>\r\n" +//
                   "</tr>" +
                   "</table>" +
                   "</td>" +
                   "</tr>" +
                   "<tr>" +
                   "    <td></td>" +
                   "    <td><hr></td>\r\n" +
                   "</tr>";
                   
             
            return Html;
        }


        [AjaxPro.AjaxMethod]
        public AdvImageItemCls[] GetAdvImageItemsObject(int MaxItems)
        {
            AdvImageItemCls[] AdvImageItems = new AdvImageItemCls[MaxItems];
            for (int iIndex = 0; iIndex < AdvImageItems.Length; iIndex++)
            {
                AdvImageItems[iIndex] = new AdvImageItemCls();
            }
            return AdvImageItems;
        }

        [AjaxPro.AjaxMethod]
        public string GetShowAppendForm(string AdvImageId)
        {
            string AppendUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new AddAdvImage().WebPartId),
                        new ParamCls("AdvImageId",AdvImageId)
                    });
            return AppendUrl;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideSaveListItems(
            string ActiveLanguage,
            string AdvImageId,
            AdvImageItemCls[] AdvImageItems)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new AdvImage().PermissionTypes, "AdvImage");
                AdvImageCls AdvImageCls = new AdvImageBLL().CreateModel(Environments.ActiveLanguage, AdvImageId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UpdateAdvImage", AdvImageId, AdvImageCls.Owner, true);



                new AdvImageBLL().SaveAdvImageItems(null, ActiveLanguage, AdvImageId, AdvImageItems);
                return ResourceBLL.GetText(WebPartId, "MessageUpdateOk", "Cập nhật thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerGetDescription(string ActiveLanguage, string AdvImageId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                AdvImageCls AdvImageCls = new AdvImageBLL().CreateModel(ActiveLanguage, AdvImageId);
                return AdvImageCls.Description;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmDeleteMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmDeleteMsg", "Bạn đã chắc chắn xóa chưa?");
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmPublishingMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmPublishingMsg", "Bạn đã chắc chắn phát hành chưa?");
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmSyncMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmSyncMsg", "Bạn đã chắc chắn cập nhật máy chủ chưa?");
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmUnPublishingMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmUnPublishingMsg", "Bạn đã chắc chắn ngừng phát hành chưa?");
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideCheckDeleteAdvImageItem(string AdvImageItemId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);

                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new AdvImage().PermissionTypes, "AdvImageItems");
                AdvImageItemCls AdvImageItemCls = new AdvImageBLL().CreateModelItem(Environments.ActiveLanguage, AdvImageItemId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "DeleteAdvImageItem", AdvImageItemCls.AdvImageItemId, AdvImageItemCls.Owner, true);
                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideDeleteAdvImageItem(string AdvImageItemId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);

                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new AdvImage().PermissionTypes, "AdvImageItems");
                AdvImageItemCls AdvImageItemCls = new AdvImageBLL().CreateModelItem(Environments.ActiveLanguage, AdvImageItemId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "DeleteAdvImageItem", AdvImageItemCls.AdvImageItemId, AdvImageItemCls.Owner, true);

                new AdvImageBLL().DeleteAdvImageItem(null, AdvImageItemId);
                return ResourceBLL.GetText(WebPartId, "DeleteOk", "Xóa thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetAddAdvImageItemUrl(string AdvImageId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new AdvImage().PermissionTypes, "AdvImage");
                AdvImageCls AdvImageCls = new AdvImageBLL().CreateModel(Environments.ActiveLanguage, AdvImageId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "AddAdvImageItem", AdvImageCls.AdvImageId, AdvImageCls.Owner, true);

                string AppendUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new AdvUploadImage().WebPartId),
                        new ParamCls("AdvImageId",AdvImageId)
                    });
                return AppendUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideSaveAdvImageItem(
            string AdvImageItemId,
            string Alias,
            string AdvImageItemName,
            string Url,
            string Target,
            string Description,
            string IndexOrder)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new AdvImage().PermissionTypes, "AdvImageItems");
                AdvImageItemCls AdvImageItemCls = new AdvImageBLL().CreateModelItem(Environments.ActiveLanguage, AdvImageItemId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "SaveAdvImageItem", AdvImageItemCls.AdvImageItemId, AdvImageItemCls.Owner, true);

                new AdvImageBLL().SaveOneAdvImageItem(null, Environments.ActiveLanguage, AdvImageItemId, Alias, AdvImageItemName, Url, Target, Description, IndexOrder);
                return ResourceBLL.GetText(WebPartId, "SaveOk", "Cập nhật thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                string Url = Environment.Environments.GetActiveThemeUrl() + "/Global/Loading.gif";
                return "<img src=\"" + Url + "\"><a class=cssProcessing>" + ResourceBLL.GetText(WebPartId, "ProcessingText", "Đang đọc dữ liệu...") + "</a>";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
        [AjaxPro.AjaxMethod]
        public string ServerSideCheckPublishing(string AdvImageItemId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);

                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new AdvImage().PermissionTypes, "AdvImageItems");
                AdvImageItemCls AdvImageItemCls = new AdvImageBLL().CreateModelItem(Environments.ActiveLanguage, AdvImageItemId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "Publishing", AdvImageItemCls.AdvImageItemId, AdvImageItemCls.Owner, true);

                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string ServerSidePublishing(string AdvImageItemId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);

                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new AdvImage().PermissionTypes, "AdvImageItems");
                AdvImageItemCls AdvImageItemCls = new AdvImageBLL().CreateModelItem(Environments.ActiveLanguage, AdvImageItemId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "Publishing", AdvImageItemCls.AdvImageItemId, AdvImageItemCls.Owner, true);

                new AdvImageBLL().Publishing(null, AdvImageItemId);
                return ResourceBLL.GetText(WebPartId, "PublishingOk", "Phát hành thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideCheckUnPublishing(string AdvImageItemId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);

                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new AdvImage().PermissionTypes, "AdvImageItems");
                AdvImageItemCls AdvImageItemCls = new AdvImageBLL().CreateModelItem(Environments.ActiveLanguage, AdvImageItemId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UnPublishing", AdvImageItemCls.AdvImageItemId, AdvImageItemCls.Owner, true);

                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideUnPublishing(string AdvImageItemId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new AdvImage().PermissionTypes, "AdvImageItems");
                AdvImageItemCls AdvImageItemCls = new AdvImageBLL().CreateModelItem(Environments.ActiveLanguage, AdvImageItemId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UnPublishing", AdvImageItemCls.AdvImageItemId, AdvImageItemCls.Owner, true);

                new AdvImageBLL().UnPublishing(null, AdvImageItemId);
                return ResourceBLL.GetText(WebPartId, "UnPublishingOk", "Ngừng phát hành thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetPublishingCaption()
        {
            try
            {
                return ResourceBLL.GetText(WebPartId, "Publishing", "Phát hành");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetUnPublishingCaption()
        {
            try
            {
                return ResourceBLL.GetText(WebPartId, "UnPublishing", "Ngừng phát hành");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetUpdatePictureItemsUrl(string AdvImageItemId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);

                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new AdvImage().PermissionTypes, "AdvImageItems");
                AdvImageItemCls AdvImageItemCls = new AdvImageBLL().CreateModelItem(Environments.ActiveLanguage, AdvImageItemId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "ReUploadAdvImageItem", AdvImageItemId, AdvImageItemCls.Owner, true);

                return Screen.BuildDialogUrl(new ReAdvUploadImage().WebPartId, new ParamCls[]{
                  new ParamCls("AdvImageItemId",AdvImageItemId)
              });
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        //[AjaxPro.AjaxMethod]
        //public string ServerSideSyncWithServer(string AdvImageId)
        //{
        //    try
        //    {
        //        Environments.CheckSessionTimeOut(true);

        //        PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new AdvImage().PermissionTypes, "AdvImage");
        //        AdvImageCls AdvImageCls = new AdvImageBLL().CreateModel(Environments.ActiveLanguage, AdvImageId);
        //        Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "SyncWithServer", AdvImageId, AdvImageCls.Owner, true);


        //        new AdvImageBLL().SyncWithServer(null, Environments.ActiveLanguage, AdvImageId);
        //        return ResourceBLL.GetText(WebPartId, "SyncOk", "Cập nhật máy chủ ok");
        //    }
        //    catch (Exception ex)
        //    {
        //        return "ERROR: " + ex.Message.ToString();
        //    }
        //}


        [AjaxPro.AjaxMethod]
        public string GetPermissionUrlItem(string AdvImageItemId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new AdvImage().PermissionTypes, "AdvImageItems");
                AdvImageItemCls AdvImageItemCls = new AdvImageBLL().CreateModelItem(Environments.ActiveLanguage, AdvImageItemId);
                string Owner = AdvImageItemCls.Owner;
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "PermissionAdvImageItem", AdvImageItemId, Owner, true);
                return Environment.Permission.GetPermissionUrl(new AdvImage().WebPartId, AdvImageItemId, PermissionTypeCls.PermissionTypeCode);
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetUpdateHtmlUrl(string AdvImageId, string AdvImageItemId)
        {
            return Screen.BuildDialogUrl(new AdvImageHtml().WebPartId, new ParamCls[]{
                new ParamCls("AdvImageId",AdvImageId),
                new ParamCls("AdvImageItemId",AdvImageItemId),
            });
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideSyncAdvImageItem(string AdvImageId, string AdvImageItemId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                new AdvImageBLL().SyncAdvImageItem(AdvImageItemId);
                return GetText("SyncOk", "Đồng bộ với máy chủ website thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetConfirmSyncAllMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmSyncAllMsg", "Bạn đã chắc chắn update lại dữ liệu tin tức trên máy chủ từ máy local chưa?");
        }



        [AjaxPro.AjaxMethod]
        public string GetUploadAvatarUrl(string AdvImageId, string AdvImageItemId)
        {
            string AppendUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new AdvUploadAvatar().WebPartId),
                        new ParamCls("AdvImageId",AdvImageId),
                        new ParamCls("AdvImageItemId",AdvImageItemId),
                    });
            return AppendUrl;
        }
    }
}